//
//  EmployeeViewController.m
//  pb
//
//  Created by 马田成 on 2017/5/5.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "EmployeeViewController.h"

@interface EmployeeViewController ()

@property (nonatomic,strong) UIImageView *headImg;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *airportLabel;
@property (nonatomic,strong) UILabel *goodAirLabel;
@property (nonatomic,strong) UILabel *airModel;

@end

@implementation EmployeeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self leftNavHide];
    [self initView];
    [self requestData];
    self.view.backgroundColor = getColor(@"ffffff");
    
    self.searchTextField = [[UITextField alloc]init];
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(70), screenScale(30))];
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, screenScale(10), 0, screenScale(-10));
    rightBtn.titleLabel.font = get4sFont(14);
    [rightBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [rightBtn setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(loginIs) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarBtnItem;
}

- (void)requestData{

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:People_Message
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       _dataDic = [[NSMutableDictionary alloc]init];
                                       _dataDic = returnData[@"data"];
                                       [self reloadView];
                                       
                                   }else{
                                       
                                       [PublicFunction showAlert:nil message:@"没有搜索到商品" duration:5.0f];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)reloadView{

    NSString *urlGood = _dataDic[@"pic"];
    NSString *urlN = manage(AP_HOST, urlGood);
    NSURL *urlW = [NSURL URLWithString:urlN];
    [_headImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    _nameLabel.text = _dataDic[@"name"];
    _airportLabel.text = _dataDic[@"number"];
    _airModel.text = _dataDic[@"advantage"];
}

#pragma mark ----------- init ------------

- (void)initView{

    _headImg = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(255), screenScale(50), screenScale(130), screenScale(130))];
    _headImg.image = [UIImage imageNamed:@"logo"];
    _headImg.layer.cornerRadius = screenScale(65);
    _headImg.layer.borderColor = getThreadColor().CGColor;
    _headImg.layer.borderWidth = screenScale(1);
    [self.view addSubview:_headImg];
    
    _nameLabel = [[UILabel alloc]init];
//    _nameLabel.text = @"赵大哈";
    _nameLabel.textColor = getColor(@"202020");
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = get4sFont(13);
    [self.view addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headImg.mas_bottom).with.offset(screenScale(20));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(28));
    }];
    
    _airportLabel = [[UILabel alloc]init];
//    _airportLabel.text = @"NH5211314";
    _airportLabel.textColor = getColor(@"7d7d7d");
    _airportLabel.textAlignment = NSTextAlignmentCenter;
    _airportLabel.font = get4sFont(12);
    [self.view addSubview:_airportLabel];
    [_airportLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_nameLabel.mas_bottom).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(28));
    }];
    
    _goodAirLabel = [[UILabel alloc]init];
    _goodAirLabel.text = @"擅长值机机型";
    _goodAirLabel.textColor = getColor(@"333333");
    _goodAirLabel.textAlignment = NSTextAlignmentCenter;
    _goodAirLabel.font = get4sFont(12);
    [self.view addSubview:_goodAirLabel];
    [_goodAirLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_airportLabel.mas_bottom).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(28));
    }];
    
    _airModel = [[UILabel alloc]init];
//    _airModel.text = @"787/747/777/330/321";
    _airModel.textColor = getColor(@"7d7d7d");
    _airModel.textAlignment = NSTextAlignmentCenter;
    _airModel.font = get4sFont(12);
    [self.view addSubview:_airModel];
    [_airModel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_goodAirLabel.mas_bottom).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(28));
    }];
    
    UIButton *exitBtn= [[UIButton alloc]init];
    exitBtn.layer.cornerRadius = screenScale(10);
//    exitBtn.layer.masksToBounds 
    exitBtn.backgroundColor = getMainColor();
    exitBtn.titleLabel.font = get4sFont(14);
    [exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitBtn setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
    [exitBtn addTarget:self action:@selector(exitBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:exitBtn];
    [exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.right.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(88));
    }];
}

- (void)setSearchTextField:(UITextField *)searchTextField{
    
    _searchTextField = searchTextField;
    _searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _searchTextField.borderStyle = UITextBorderStyleNone;
    _searchTextField.tintColor = [UIColor blackColor];
    _searchTextField.backgroundColor = getColor(@"ffffff");
    _searchTextField.clearButtonMode = UITextFieldViewModeAlways;
    _searchTextField.layer.cornerRadius = screenScale(10);
    _searchTextField.returnKeyType = UIReturnKeySearch;
    _searchTextField.delegate = self;
    _searchTextField.layer.masksToBounds = YES;
    _searchTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入员工姓名或工号搜索" attributes:@{NSForegroundColorAttributeName: getColor(@"cecece")}];
    _searchTextField.font = get4sFont(11);
    _searchTextField.frame = CGRectMake(screenScale(0), screenScale(0), screenScale(520), screenScale(55));
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenScale(55), screenScale(55))];
    UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(10), screenScale(10), screenScale(35), screenScale(35))];
    leftView.image = [UIImage imageNamed:@"search"];
    [view addSubview:leftView];
    _searchTextField.leftView = view;
    _searchTextField.leftViewMode = UITextFieldViewModeAlways;
    [self.navigationController.navigationBar addSubview:_searchTextField];
    self.navigationItem.titleView = _searchTextField;
}

- (void)exitBtn{

    [PublicFunction removeConfigWithKey:@"token"];
    loginViewController *vc = [[loginViewController alloc]init];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)loginIs{
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Login_is
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self rightBtnClick];
                                       
                                   }else{
                                       
                                       [PublicFunction showAlert:nil message:returnData[@"msg"] duration:10.0f];
                                       
                                       loginViewController *loginVc = [[loginViewController alloc]init];
                                       [self presentViewController:loginVc animated:YES completion:nil];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)rightBtnClick{
    
    [_searchTextField endEditing:YES];

    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: _searchTextField.text forKey:@"number"];
    
    [[MNetworkHandler sharedInstance]conURL:People_Message
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       _dataDic = [[NSMutableDictionary alloc]init];
                                       _dataDic = returnData[@"data"];
                                       [self reloadView];
                                       
                                   }else{
                                       
                                       [PublicFunction showAlert:nil message:returnData[@"msg"] duration:5.0f];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == _searchTextField) {
        
        [self loginIs];
        
    }
    return YES;
}

@end
